using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Lexalytics;

namespace SalienceEngineDemo
{
    public partial class ctrlGetDocumentSentiment : UserControl, DemoControl
    {
        public RichTextBox oTextBox;

        public ctrlGetDocumentSentiment(RichTextBox oTextBox)
        {
            InitializeComponent();
            this.oTextBox = oTextBox;

            EnableProcessButton();
        }

        public void processText()
        {
            grdResults.Rows.Clear();
            
            try
            {
                SalienceDemo.oSalienceSession.PrepareText(this.oTextBox.Text);
                SalienceSentiment oResults = SalienceDemo.oSalienceSession.GetDocumentSentiment(true);

                lblSentiment.Text = Convert.ToString(oResults.fScore);

                if (oResults.Phrases.Count > 0)
                {
                    //Put the results in the grid
                    int nCount = 0;
                    foreach (SalienceSentimentPhrase oPhrase in oResults.Phrases)
                    {
                        grdResults.Rows.Add();
                        grdResults.Rows[nCount].Cells[0].Value = oPhrase.Phrase.sText;
                        grdResults.Rows[nCount].Cells[1].Value = oPhrase.fScore;
                        grdResults.Rows[nCount].Cells[2].Value = oPhrase.nType;
                        switch (oPhrase.nModified)
                        {
                            case 1:
                                grdResults.Rows[nCount].Cells[3].Value = "Negated";
                                break;
                            case 2:
                                grdResults.Rows[nCount].Cells[3].Value = "Intensified";
                                break;
                            case 3:
                                grdResults.Rows[nCount].Cells[3].Value = "All";
                                break;
                            default:
                                break;
                        }
                        grdResults.Rows[nCount].Cells[4].Value = oPhrase.sSource;
                        if (oPhrase.nModified > 0)
                        {
                            for (int i = 0; i < oPhrase.SupportingPhrases.Count; i++)
                            {
                                grdResults.Rows[nCount].Cells[5].Value += i == oPhrase.SupportingPhrases.Count - 1 ? oPhrase.SupportingPhrases[i].sText : oPhrase.SupportingPhrases[i].sText + ", ";
                            }
                        }
                        nCount++;
                    }
                    grdResults.AutoResizeColumns();
                    grdResults.AutoResizeRows();
                }

                foreach (SalienceModelSentiment oModel in oResults.ModelSentiment)
                {
                    if (oModel.sName == "default")
                    {
                        lblModelPositive.Text = Convert.ToString(oModel.fPositive);
                        lblModelNegative.Text = Convert.ToString(oModel.fNegative);
                        lblModelMixed.Text = Convert.ToString(oModel.fMixed);
                        lblModelNeutral.Text = Convert.ToString(oModel.fNeutral);
                    }
                }
            }
            catch (SalienceException e)
            {
                MessageBox.Show(e.Message, "Salience Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            processText();
            
        }

        private void getDocumentSentiment_VisibleChanged(object sender, EventArgs e)
        {
            lblSentiment.Text = "";
            grdResults.Rows.Clear();
        }

        #region DemoControl interface methods
        public void btnOptions_Click(object sender, EventArgs e)
        {
            ((SalienceDemo)this.Parent).StartOptionsDialog((int)Dialogs.Options.OptionSections.DOCUMENTS);
            EnableProcessButton();
        }

        public void SetFontSize()
        {
            this.grdResults.DefaultCellStyle.Font = new Font(this.oTextBox.Font.FontFamily, Properties.Settings.Default.fFontSize);
            this.grdResults.AutoResizeRows();
        }

        public void EnableProcessButton()
        {
            // Process button is enabled based on existence of content to process
            this.btnProcess.Enabled = (oTextBox.Text.Trim().Length > 0);
        }
        #endregion
    }
}
